package gaml.text;

import gaml.error.GamlException;
import gaml.yaml.YamlNode;


/**
 * parse text into yaml node
 * serialize yaml node into text
 * @author shnireck
 *
 */
public class TextNodeBridge {
	YamlText text = null;
	YamlNode node = null;
	
	public TextNodeBridge (YamlText text, YamlNode node) { assign (text, node); }
	
	/**
	 * yaml representating text
	 * @return yaml text object
	 */
	public YamlText getText () { return text; }
	
	/**
	 * yaml in form of internal node object
	 * @return yaml node object
	 */
	public YamlNode getNode () {return node; }
	
	/**
	 * translate specified node into yaml text
	 * @param store yaml node printers collection
	 * @return host object
	 * @throws GamlException yaml text mailformed
	 */
	public TextNodeBridge parseNode (YamlPrinterStore store) throws GamlException { 
		if (store.hasPrinter (node))  store.getPrinter (node).print (node, text);
		else store.getDefaultPrinter ().print (node, text);

		return this;
	}
	
	/**
	 * parse yaml text into yaml node object
	 * @param store yaml text parsers collection
	 * @return host object
	 * @throws GamlException yaml text mailformed
	 */
	public TextNodeBridge parseText (YamlParserStore store) throws GamlException { 
		if ((! text.isEnd ()) && (text.getIdentation () == (null == node ? 0 : (node.getLevel () + 2))) && (store.hasParser (text))) 
			node = store.getParser (text).parseText (text, node);
		else node = store.getDefaultParser ().parseText (text, node);
		
		return this;
	}

	protected void assign (YamlText text, YamlNode node) {
		this.text = text;
		this.node = node;
	}
}